package com.gitee.aurora.core.utils;

import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MapToObj {

    /**
     * 把source转为target
     *
     * @param source source
     * @param target target
     * @param <T>    返回值类型
     * @return 返回值
     * @throws Exception newInstance可能会抛出的异常
     */
    public static <T> T mapToObj(Map source, Class<T> target) {
        try{
            Field[] fields = target.getDeclaredFields();
            T o = target.newInstance();
            for (Field field : fields) {
                Object val;
                if ((val = source.get(field.getName())) != null) {
                    field.setAccessible(true);
                    field.set(o, val);
                }
            }
            return o;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T mapUpperCaseToObj(Map source, Class<T> target) {
        try{
            Field[] fields = target.getDeclaredFields();
            T o = target.newInstance();
            for (Field field : fields) {
                Object val;
                if ((val = source.get(field.getName().toUpperCase())) != null) {
                    field.setAccessible(true);
                    field.set(o, val);
                }
            }
            return o;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 把source转为target
     *
     * @param source source
     * @param target target
     * @param <T>    返回值类型
     * @return 返回值
     * @throws Exception newInstance可能会抛出的异常
     */
    public static <T> T mapUpperCaseFieldToObj(Map source, Class<T> target) {
        try{
            Field[] fields = target.getDeclaredFields();
            T o = target.newInstance();
            for (Field field : fields) {
                Object val;
                String s = field.getName().toUpperCase();
                if("SID".equals(s)||"S_ID".equals(s)){
                    s = "sId";
                }
                if ((val = source.get(s)) != null) {
                    field.setAccessible(true);
                    if(StringUtils.isEmpty(val)){
                        field.set(o, null);
                        if("SFSC".equals(s)){
                            field.set(o, "0");
                        }
                    }else {
                        field.set(o, val.toString());
                    }
                }
            }
            return o;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对象转Map
     * @param object
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> ObjToMap(Object object) {
        try{
            Map<String, Object> map = new HashMap<>();
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(object));
            }
            return map;
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }
       return null;
    }
}